#region OpenNETCF Copyright Information
/*
Copyright (c) 2007 OpenNETCF Consulting, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial 
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
#if Fx
using Microsoft.Ink;
using System.IO;
#endif
namespace OpenNETCF.WindowsMobile.Ink
{
    /// <summary>
    /// Signature control using Windows Mobile 6 Inking APIs
    /// </summary>
    public class InkSignature : InkPicture
    {
        private string m_sigValidationGuid = "{1AE79181-7B9E-45ec-B525-20023752860A}";
        private string m_firstNameGuid = "{1AE79181-7B9E-45ec-B525-20023752860B}";
        private string m_lastNameGuid = "{1AE79181-7B9E-45ec-B525-20023752860C}";
        private string m_dateSignedGuid = "{1AE79181-7B9E-45ec-B525-20023752860D}";

        private string m_firstname = "";


        private string m_lastname = "";


        private DateTime m_dateSigned = DateTime.Now;



        public InkSignature()
        {
        }
#if !Fx
        [Obsolete("Use image instead to be compatible with the desktop.")]
        public Image BackgroundImage
        {
            get
            {
                return m_pictureBox.Image;
            }
            set
            {
                m_pictureBox.Image = value;
                this.Invalidate();
            }
        }
#endif


        public string Firstname
        {
            get { return m_firstname; }
            set { m_firstname = value; }
        }

        public string Lastname
        {
            get { return m_lastname; }
            set { m_lastname = value; }
        }

        public DateTime DateSigned
        {
            get { return m_dateSigned; }
            set { m_dateSigned = value; }
        }

#if !Fx
public override void Open(string file)
#else
        public void Open(string file)
#endif
        {
#if !Fx
    base.Open(file);
#else
            this.InkEnabled = false;
            StreamReader sr = new StreamReader(file);
            byte[] data = Encoding.ASCII.GetBytes(sr.ReadToEnd());
            sr.Close();
            this.Ink = new Microsoft.Ink.Ink();
            this.Ink.Load(data);
            this.Refresh();
#endif

            //See if the ISF file opened is valid
            object ret = this.Ink.ExtendedProperties[new Guid(m_sigValidationGuid)].Data;
            if (ret == null)
            {
#if !Fx
        this.Clear();
#else
                this.InkEnabled = false;
                this.Ink = new Microsoft.Ink.Ink();
                this.InkEnabled = true;
#endif
                this.m_firstname = "";
                this.m_lastname = "";
                this.m_dateSigned = DateTime.MinValue;
                throw new Exception("Invalid signature file!");
            }

            //Retreive the firstname extended properties
            ret = this.Ink.ExtendedProperties[new Guid(m_firstNameGuid)].Data;

            this.m_firstname = ret.ToString();

            //Retreive the lastName extended properties
            ret = this.Ink.ExtendedProperties[new Guid(m_lastNameGuid)].Data;
            this.m_lastname = ret.ToString();

            //Retreive the date the sign was signed
            ret = this.Ink.ExtendedProperties[new Guid(m_dateSignedGuid)].Data;
            this.m_dateSigned = DateTime.FromFileTime(long.Parse(ret.ToString()));
        }
        //NOTE Saving on the desktop is not supported and is only used for viewing
#if !Fx
        public void Save(string filename, string firstName, string lastName, InkPersistenceFormat format)
        {
            this.Firstname = firstName;
            this.Lastname = lastName;

            //Get the current Date
            DateSigned = DateTime.Now;

            //Add the extended properties before saving
            List<ExtendedProperty> props = new List<ExtendedProperty>();
            props.Add(new ExtendedProperty(m_sigValidationGuid, "OpenNETCF"));
            props.Add(new ExtendedProperty(m_firstNameGuid, Firstname));
            props.Add(new ExtendedProperty(m_lastNameGuid, Lastname));
            props.Add(new ExtendedProperty(m_dateSignedGuid, DateSigned.ToFileTime().ToString()));

            base.Save(filename, props,format);
        }
        public void Save(string filename, string firstName, string lastName)
        {
            this.Save(filename, firstName, lastName, InkPersistenceFormat.IPF_InkSerializedFormat);
        }
#endif
    }
}
